#-------------------------------------------------------------------------------
# Name:        Button
# Purpose:
#
# Author:      Pablo
#
# Created:     23/01/2012
# Copyright:   (c) Monxcleyr Productions 2012
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, os


class Generic():

  """ Generic Button class.

  Generic(screen surface, button origin(x,y), button length, text font, button text, toggleable(bool), \
tooltip font, tooltip text, icon.png)

  Use Generic.state() to return state (1 or 0)

  Use Generic.setState(state) to force state (1 or 0)"""

  def __init__(self, screen, pos, size, buttonfont, text, toggle, toolfont, tooltip, icon):
    self.screen = screen
    self.pos = pos
    self.size = size
    self.font = buttonfont
    self.text = text
    self.toggle = toggle
    self.toolfont = toolfont
    self.tooltip = tooltip
    if icon != None:
      self.icon = pygame.image.load(os.path.join('resources/graphics/gui/' + str(icon) + '.bmp'))
      self.icon.set_colorkey((255,0,255))
    else:
      self.icon = None

    if self.text != None:
      size = self.font.size(self.text)
      self.size = size[0] + 24

    self.clicked = 0
    self.toggled = 0
    self.hover = 0
    self.count = 4
    self.pos2 = (self.pos[0] + self.size - 12, self.pos[1])
    self.button1 = pygame.image.load(os.path.join('resources/graphics/gui/buttona1.bmp'))
    self.button1.set_colorkey((255,0,255))
    self.button2 = pygame.image.load(os.path.join('resources/graphics/gui/buttona2.bmp'))
    self.button2.set_colorkey((255,0,255))
    self.tool1 = pygame.image.load(os.path.join('resources/graphics/gui/tooltip1.bmp'))
    self.tool1.set_colorkey((255,0,255))
    self.tool2 = pygame.image.load(os.path.join('resources/graphics/gui/tooltip2.bmp'))
    self.tool2.set_colorkey((255,0,255))
    self.rect = pygame.Rect(self.pos[0], self.pos[1], self.size, 25)

  def draw(self, mousepos, leftclick, leftup):

    """Draw Buttons.
    Returns clicked state. (0 or 1)"""

    self.mousepos = mousepos

    if self.count != 0:
      self.count -= 1

    #draw button
    buttonsurf = pygame.Surface((self.size,25), flags = pygame.SRCALPHA)
    buttonsurf2 = pygame.Surface((12,25), flags = pygame.SRCALPHA)
    if self.toggle == 0:
      if self.rect.collidepoint(mousepos) == True and leftclick == True and leftup != True:
        buttonsurf.blit(self.button1, (0,0), (0, 50, (self.size - 12), 25))
        buttonsurf2.blit(self.button2, (0,0), (0, 50, 12, 25))
        self.clicked = 1
      elif self.rect.collidepoint(mousepos) == True:
        buttonsurf.blit(self.button1, (0,0), (0, 25, (self.size - 12), 25))
        buttonsurf2.blit(self.button2, (0,0), (0, 25, 12, 25))
        self.hover = 1
        self.clicked = 0
      else:
        buttonsurf.blit(self.button1, (0,0), (0, 0, (self.size - 12), 25))
        buttonsurf2.blit(self.button2, (0,0), (0, 0, 12, 25))
        self.clicked = 0
        self.hover = 0
    elif self.toggle > 0:
      if self.rect.collidepoint(mousepos) == True and leftclick == True and leftup != True:
        if self.toggled > 0:
          buttonsurf.blit(self.button1, (0,0), (0, 50, (self.size - 12), 25))
          buttonsurf2.blit(self.button2, (0,0), (0, 50, 12, 25))
          if self.count == 0:
            self.toggled = 0
            self.count = 4
        else:
          buttonsurf.blit(self.button1, (0,0), (0, 0, (self.size - 12), 25))
          buttonsurf2.blit(self.button2, (0,0), (0, 0, 12, 25))
          if self.count  == 0:
            self.toggled = 1
            self.count = 4
      elif self.rect.collidepoint(mousepos) == True:
        buttonsurf.blit(self.button1, (0,0), (0, 25, (self.size - 12), 25))
        buttonsurf2.blit(self.button2, (0,0), (0, 25, 12, 25))
        self.hover = 1
        self.clicked = 0
      elif self.toggled == 0:
        buttonsurf.blit(self.button1, (0,0), (0, 0, (self.size - 12), 25))
        buttonsurf2.blit(self.button2, (0,0), (0, 0, 12, 25))
        self.hover = 0
      elif self.toggled > 0:
        buttonsurf.blit(self.button1, (0,0), (0, 50, (self.size - 12), 25))
        buttonsurf2.blit(self.button2, (0,0), (0, 50, 12, 25))
        self.hover = 0

    self.screen.blit(buttonsurf, self.pos)
    self.screen.blit(buttonsurf2, self.pos2)

    # draw text
    if self.text != None:
      text2 = self.font.render(self.text, True, (255, 255, 255))
      if self.toggle != True:
        if self.rect.collidepoint(mousepos) == True and leftclick == True:
          self.screen.blit(text2, (self.pos[0] + 13, self.pos[1] + 6))
        else:
          self.screen.blit(text2, (self.pos[0] + 12, self.pos[1] + 5))
      elif self.toggle == True:
        if self.toggled == 1:
          self.screen.blit(text2, (self.pos[0] + 13, self.pos[1] + 6))
        else:
          self.screen.blit(text2, (self.pos[0] + 12, self.pos[1] + 5))

    if self.icon != None:
      self.screen.blit(self.icon, (self.pos[0] + 3, self.pos[1] + 2))

    # Return the button's click state
    if self.toggle != True:
      return self.clicked
    else:
      return self.toggled

  def state(self):

    # Return the button's click state
    if self.toggle != True:
      return self.clicked
    else:
      return self.toggled

  def setState(self, state):

    # Set the button's click state
    if self.toggle != True:
      self.clicked = state
    else:
      self.toggled = state


  def setText(self, text):

    """ Reset Text. """

    size = self.font.size(text)
    size = size[0] + 24
    self.size = size
    self.text = text
    self.pos2 = (self.pos[0] + self.size - 12, self.pos[1])
    self.rect = pygame.Rect(self.pos[0], self.pos[1], self.size, 25)


  def drawTool(self):

    """ Draw Tooltips. """

    if self.hover == 1 and self.tooltip != None:
      size = self.toolfont.size(self.tooltip)
      size = size[0] + 12
      tool = self.toolfont.render(self.tooltip, True, (255, 255, 255))

      toolsurf = pygame.Surface((size,20), flags = pygame.SRCALPHA)
      toolsurf2 = pygame.Surface((6,20), flags = pygame.SRCALPHA)

      toolsurf.blit(self.tool1, (0,0), (0, 0, (size + 10), 20))
      toolsurf2.blit(self.tool2, (0,0), (0, 0, 12, 20))

      self.screen.blit(toolsurf, (self.mousepos[0] + 10, self.mousepos[1] + 10))
      self.screen.blit(toolsurf2, (self.mousepos[0] + size + 7, self.mousepos[1] + 10))

      self.screen.blit(tool, (self.mousepos[0] + 16, self.mousepos[1] + 16))


#-------------------------------------------------------------------------------


class Checkbox():

  """ Checkbox Button class.

  Checkbox(screen surface, button origin(x,y), tooltip font, tooltip text)

  Use Checkbox.state() to check state (1 or 0)

  Use Checkbox.setState(state) to force state (1 or 0)"""

  def __init__(self, screen, pos, toolfont, tooltip):
    self.screen = screen
    self.pos = pos
    self.toolfont = toolfont
    self.tooltip = tooltip

    self.clicked = 0
    self.toggled = 0
    self.hover = 0
    self.count = 4
    self.check = pygame.image.load(os.path.join('resources/graphics/options/checkbox.bmp'))
    self.check.set_colorkey((255,0,255))
    self.tool1 = pygame.image.load(os.path.join('resources/graphics/options/tooltip1.bmp'))
    self.tool1.set_colorkey((255,0,255))
    self.tool2 = pygame.image.load(os.path.join('resources/graphics/options/tooltip2.bmp'))
    self.tool2.set_colorkey((255,0,255))
    self.rect = pygame.Rect(self.pos[0], self.pos[1], 16, 16)

  def draw(self, mousepos, leftclick, leftup):

    """Draw Buttons.
    Returns clicked state. (0 or 1)"""

    self.mousepos = mousepos

    if self.count != 0:
      self.count -= 1

    #draw button
    buttonsurf = pygame.Surface((16,16), flags = pygame.SRCALPHA)

    if self.rect.collidepoint(mousepos) == True and leftclick == True and leftup != True:
      if self.toggled > 0:
        buttonsurf.blit(self.check, (0,0), (32, 0, 16, 16))
        if self.count == 0:
          self.toggled = 0
          self.count = 4
      else:
        buttonsurf.blit(self.check, (0,0), (0, 0, 16, 16))
        if self.count  == 0:
          self.toggled = 1
          self.count = 4
    elif self.rect.collidepoint(mousepos) == True:
      if self.toggled > 0:
        buttonsurf.blit(self.check, (0,0), (48, 0, 16, 16))
      else:
        buttonsurf.blit(self.check, (0,0), (16, 0, 16, 16))
      self.hover = 1
      self.clicked = 0
    elif self.toggled == 0:
      buttonsurf.blit(self.check, (0,0), (0, 0, 16, 16))
      self.hover = 0
    elif self.toggled > 0:
      buttonsurf.blit(self.check, (0,0), (32, 0, 16, 16))
      self.hover = 0

    self.screen.blit(buttonsurf, self.pos)

    # Return the button's click state
    return self.toggled

  def drawTool(self):

    """ Draw Tooltips. """

    if self.hover == 1 and self.tooltip != None:
      size = self.toolfont.size(self.tooltip)
      size = size[0] + 12
      tool = self.toolfont.render(self.tooltip, True, (255, 255, 255))

      toolsurf = pygame.Surface((size,20), flags = pygame.SRCALPHA)
      toolsurf2 = pygame.Surface((6,20), flags = pygame.SRCALPHA)

      toolsurf.blit(self.tool1, (0,0), (0, 0, (size + 10), 20))
      toolsurf2.blit(self.tool2, (0,0), (0, 0, 12, 20))

      self.screen.blit(toolsurf, (self.mousepos[0] + 10, self.mousepos[1] + 10))
      self.screen.blit(toolsurf2, (self.mousepos[0] + size + 7, self.mousepos[1] + 10))

      self.screen.blit(tool, (self.mousepos[0] + 16, self.mousepos[1] + 16))

  def state(self):

    # Return the button's click state
    return self.toggled

  def setState(self, state):

    # Set the button's click state
    self.toggled = state
#-------------------------------------------------------------------------------


class Radio():

  """ Radio Button class.

  Radio(screen surface, button origin(x,y)[List], tooltip font, tooltip text[List])

  Use Radio.state() to check state. Returns list index

  Use Radio.setState(state) to forcee state. (list index)"""

  def __init__(self, screen, pos, toolfont, tooltip):
    self.screen = screen
    self.pos = pos
    self.toolfont = toolfont
    self.tooltip = tooltip

    self.clicked = 0
    self.toggled = 0
    self.hover = 0
    self.count = 4
    self.rectlist = []
    self.selected = 0
    self.state = 0
    self.check = pygame.image.load(os.path.join('resources/graphics/gui/radio.bmp'))
    self.check.set_colorkey((255,0,255))
    self.tool1 = pygame.image.load(os.path.join('resources/graphics/gui/tooltip1.bmp'))
    self.tool1.set_colorkey((255,0,255))
    self.tool2 = pygame.image.load(os.path.join('resources/graphics/gui/tooltip2.bmp'))
    self.tool2.set_colorkey((255,0,255))
    for i in range(0, len(pos)):
      self.rectlist.append(pygame.Rect(self.pos[i][0], self.pos[i][1], 16, 16))

  def draw(self, mousepos, leftclick, leftup):

    """Draw Buttons.

    Returns list index of selected button"""

    self.mousepos = mousepos

    if self.count != 0:
      self.count -= 1

    for i in range(0, len(self.pos)):
      #check state
      if self.rectlist[i].collidepoint(mousepos) == True and leftclick == True and leftup != True:
        if self.selected == i:
          if self.count == 0:
            self.count = 4
            self.state = 1
        else:
          if self.count == 0:
            self.count = 4
            self.state = 2
      elif self.rectlist[i].collidepoint(mousepos) == True:
        if self.selected == i:
          self.state = 3
        else:
          self.state = 4
        self.hover = 1
        self.clicked = 0
      elif self.selected != i:
        self.state = 5
        self.hover = 0
      elif self.selected == i:
        self.state = 6
        self.hover = 0

      if self.state == 1 or self.state == 2:
        self.selected = i

      #draw buttosn based on state
      buttonsurf = pygame.Surface((16,16), flags = pygame.SRCALPHA)

      if self.state == 1:
        buttonsurf.blit(self.check, (0,0), (32, 0, 16, 16))
      elif self.state == 2:
        buttonsurf.blit(self.check, (0,0), (0, 0, 16, 16))
      elif self.state == 3:
        buttonsurf.blit(self.check, (0,0), (48, 0, 16, 16))
      elif self.state == 4:
        buttonsurf.blit(self.check, (0,0), (16, 0, 16, 16))
      elif self.state == 5:
        buttonsurf.blit(self.check, (0,0), (0, 0, 16, 16))
      elif self.state == 6:
        buttonsurf.blit(self.check, (0,0), (32, 0, 16, 16))

      self.screen.blit(buttonsurf, self.pos[i])

    # Return the button's click state
    return self.selected

  def state(self):

    # Return the button's click state
    return self.selected

  def setState(self, state):

    # Set the button's click state
    self.selected = state

  def drawTool(self):

    """ Draw Tooltips. """

    for i in range(0, len(self.pos)):
      if self.rectlist[i].collidepoint(self.mousepos) == True and self.tooltip != None:
        size = self.toolfont.size(self.tooltip[i])
        size = size[0] + 12
        tool = self.toolfont.render(self.tooltip[i], True, (255, 255, 255))

        toolsurf = pygame.Surface((size,20), flags = pygame.SRCALPHA)
        toolsurf2 = pygame.Surface((6,20), flags = pygame.SRCALPHA)

        toolsurf.blit(self.tool1, (0,0), (0, 0, (size + 10), 20))
        toolsurf2.blit(self.tool2, (0,0), (0, 0, 12, 20))

        self.screen.blit(toolsurf, (self.mousepos[0] + 10, self.mousepos[1] + 10))
        self.screen.blit(toolsurf2, (self.mousepos[0] + size + 7, self.mousepos[1] + 10))

        self.screen.blit(tool, (self.mousepos[0] + 16, self.mousepos[1] + 16))
